<!-- src/components/Dialog.vue -->
<template>
  <teleport to="body">
    <div v-if="visible" class="dialog-overlay" @click.self="close">
      <div class="dialog-container" :class="{ 'dialog-enter': visible }">
        <!-- 标题 -->
        <div class="dialog-title">{{ title }}</div>

        <!-- 内容 -->
        <div class="dialog-message">{{ message }}</div>

        <!-- 按钮 -->
        <div class="dialog-actions">
          <button class="dialog-btn dialog-cancel" @click="cancel">取消</button>
          <button class="dialog-btn dialog-confirm" @click="confirm">确认</button>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script setup>
import { ref } from 'vue'

const props = defineProps({
  title: { type: String, default: '提示' },
  message: { type: String, default: '' },
  onConfirm: { type: Function, default: () => { } },
  onCancel: { type: Function, default: () => { } }
})

const visible = ref(false)

const show = () => {
  visible.value = true
}

const close = () => {
  // 如果你想退出也有动画，可以加 class，但简单场景直接隐藏也行
  visible.value = false
}

const confirm = () => {
  props.onConfirm()
  close()
}

const cancel = () => {
  props.onCancel()
  close()
}

defineExpose({ show, close })
</script>

<style lang="scss" scoped>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

/* 定义进入动画 */
@keyframes dialog-pop-in {
  from {
    transform: scale(0.3);
    opacity: 0;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

.dialog-container {
  background: #fff;
  border-radius: 12px;
  width: 80%;
  max-width: 400px;
  padding: 24px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);

  /* 应用动画 */
  animation: dialog-pop-in 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;

  /* 初始状态（保险） */
  transform: scale(0.3);
  opacity: 0;
}

/* 退出动画（可选，用 JS 控制） */
.dialog-container-exit {
  animation: dialog-pop-out 0.3s ease-in forwards;
}

@keyframes dialog-pop-out {
  from {
    transform: scale(1);
    opacity: 1;
  }

  to {
    transform: scale(0.3);
    opacity: 0;
  }
}

.dialog-title {
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 16px;
}

.dialog-message {
  color: #666;
  line-height: 1.5;
  margin-bottom: 20px;
  text-align: center;
}

.dialog-actions {
  display: flex;
  gap: 10px;
}

.dialog-btn {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
}

.dialog-cancel {
  background: #f0f0f0;
  color: #333;
}

.dialog-confirm {
  background: #409eff;
  color: white;
}
</style>